package net.helix.ui.panels.timeline;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.HierarchyBoundsListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.ArrayList;

import javax.swing.JPanel;

import net.helix.engine.Engine;
import net.helix.engine.timeline.frame.TimelimeFrameType;
import net.helix.engine.timeline.frame.TimelineFrame;
import net.helix.engine.timeline.frame.TimelineKeyFrame;
import net.helix.engine.timeline.layer.TimelineLayerButton;
import net.helix.ui.UIImages;

public class TimelineFramesPane extends JPanel {
	private static final long serialVersionUID = 1L;

	
	public int scrollX = 0;
	public int scrollY = 0;
	
	private int mouseX = -1;
	private int mouseY = -1;
	
	private Engine engine;
	private TimelinePane timelinePane;
	public TimelineFramesPane(Engine engine, TimelinePane timelinePane){
		super();
		this.engine = engine;
		this.timelinePane = timelinePane;
		
		this.addFocusListener(new FocusListener(){

			@Override
			public void focusGained(FocusEvent arg0) {
				repaint();
			}
			@Override
			public void focusLost(FocusEvent arg0) {
				repaint();
			}
		});
		
		this.addHierarchyListener(new HierarchyListener(){
			@Override
			public void hierarchyChanged(HierarchyEvent arg0) {
				
			}
			
		});
		
		this.addHierarchyBoundsListener(new HierarchyBoundsListener(){

			@Override
			public void ancestorMoved(HierarchyEvent arg0) {
				
			}

			@Override
			public void ancestorResized(HierarchyEvent arg0) {
			}
			
		});
		
		this.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent arg0) {
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
			}
			
		});
		
		this.addMouseListener(new MouseListener(){

			@Override
			public void mouseClicked(MouseEvent arg0) {
				
			}

			@Override
			public void mouseEntered(MouseEvent arg0) {
			}

			@Override
			public void mouseExited(MouseEvent arg0) {
			}

			@Override
			public void mousePressed(MouseEvent arg0) {
				grabFocus();
			}

			@Override
			public void mouseReleased(MouseEvent arg0) {
			}
			
		});
		
		this.addMouseMotionListener(new MouseMotionListener(){

			@Override
			public void mouseDragged(MouseEvent arg0) {
			}

			@Override
			public void mouseMoved(MouseEvent arg0) {
				mouseX = arg0.getX();
				mouseY = arg0.getY();
				repaint();
			}
			
		});
		
		this.addMouseWheelListener(new MouseWheelListener(){

			@Override
			public void mouseWheelMoved(MouseWheelEvent arg0) {
				
			}
			
		});
		
		this.addComponentListener(new ComponentListener(){
			@Override
			public void componentHidden(ComponentEvent arg0) {
				
			}

			@Override
			public void componentMoved(ComponentEvent arg0) {
				
			}

			@Override
			public void componentResized(ComponentEvent arg0) {
				//paint(getGraphics());
				//update(getGraphics());
			}
			@Override
			public void componentShown(ComponentEvent arg0) {
				
			}
			
			
		});
		
		
	}
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		
		//timelinePane.UpdateTimelineScrollbars();
		
		Dimension d = this.getSize();
		if(d.width <= 0 || d.height <= 0) return;
		
		if(hasFocus()){
			g.setColor(new Color(0xF5F5F5));
		}else{
			g.setColor(new Color(0xEEEEEE));
		}
		g.fillRect(0, 0, d.width, d.height);
		
		if(engine.getCurrentSelectedTimeline() == null) return;
		
		int offX = scrollX%8;
		int offY = scrollY%20;
		
		Color frameSeparator = new Color(0x000000);
		Color frameSeparator2blank = new Color(0x7F7F7F);
		Color frameSeparator2filled = new Color(0x494949);
		
		
		int layerIndex = (scrollY - offY)/20;
		for(int y=-offY;y<d.height;y+=20){
			
			ArrayList<TimelineLayerButton> layers = timelinePane.getListOfLayers();
			
			TimelineLayerButton layerButton = null;
			if(layerIndex >= 0 && layerIndex < layers.size()){
				layerButton = layers.get(layerIndex);
			}
			
			if(layerButton != null){
				g.setColor(new Color(0x121212));
				
				g.drawLine(0, y, d.width, y);
				
				g.setColor(new Color(0xD2D2D2));
				g.drawLine(0, y+19, d.width, y+19);
				
				int frameIndex = (scrollX - offX)/8 - 1;
				
				boolean lastFrameSolid = true;
				for(int x=-offX - 8;x<d.width;x+=8){
					TimelineFrame frame = null;
					
					if(frameIndex >= 0 && frameIndex < layerButton.layer.frames.size()){
						frame = layerButton.layer.frames.get(frameIndex);
					}
					if(frameIndex >= layerButton.layer.frames.size()) break;
					if(frame != null){
						if(frame.type == TimelimeFrameType.KEYFRAME){
							TimelineKeyFrame kFrame = (TimelineKeyFrame) frame;
							
							if(kFrame.isEmpty()){
								g.drawImage(UIImages.keyFrame, x, y, null);
							}else{
								g.drawImage(UIImages.emptyKeyFrame, x, y, null);
							}
							
							lastFrameSolid = true;
						}
						if(frame.type == TimelimeFrameType.FRAME){
							if(frame.isEmpty()){
								g.drawImage(UIImages.emptyFrame, x, y, null);
								if(lastFrameSolid){ // lastFrameSolid
									g.setColor(frameSeparator);
									g.drawLine(x, y, x, y+19);
									
									g.setColor(frameSeparator2blank);
									g.drawLine(x+1, y, x+1, y+19);
								}
							}else{
								g.drawImage(UIImages.frame, x, y, null);
								if(lastFrameSolid){ // lastFrameSolid
									g.setColor(frameSeparator);
									g.drawLine(x, y, x, y+19);
									
									g.setColor(frameSeparator2filled);
									g.drawLine(x+1, y, x+1, y+19);
								}
								
							}
							lastFrameSolid = false;
							
						}
						/*
						if(frameNum == 2){
							g.drawImage(UIImages.keyFrame, x, y, null);
						}
						if(frameNum == 3){
							g.drawImage(UIImages.tweenKeyFrame, x, y, null);
						}
						*/
					}
					frameIndex++;
				}
			}
			layerIndex++;
		}
	}
	
}
